/**
 * 
 */
package com.jewaton.auth.commons.service.impl;

import com.jewaton.auth.commons.dao.ConfigDAO;
import com.jewaton.auth.commons.entity.Config;
import com.jewaton.auth.commons.service.ConfigService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**   
 *    
 * 配置表service实现类
 *
 */
@Service("configService")
public class ConfigServiceImpl implements ConfigService {
	@Resource
	private ConfigDAO configDAO;
	
	@Override
	@Cacheable(value = "configStr", key="#key")
	public String getValue(String key) {
		Config config = this.getConfig(key);
		return config!=null?config.getValue():null;
	}
	
	@Override
	@Cacheable(value = "config", key="#key")
	public Config getConfig(String key){
	    return configDAO.getByKey(key);
	}

    @Override
    public Config getConfigNoCache(String key){
        return configDAO.getByKey(key);
    }

    @Override
	@CacheEvict(value={"config","configStr","configInt"},key="#config.key")
	public void update(Config config){
        configDAO.update(config);
	}

	@Override
	@Cacheable(value = "configInt", key="#key")
	public Integer getIntValue(String key) {
		String value = getValue(key);
		try {
			return Integer.parseInt(value);
		}catch (Exception e) {
			return null;
		}
	}
	
	public void setConfigDAO(ConfigDAO configDAO) {
		this.configDAO = configDAO;
	}
	
}
